﻿using EquipSecs300;
using SecsEquip;
using SecsEquip300;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EquipSecs300
{
    public class GEM_Alarm: SecsEquipEx
    {

        public string m_pViewName;
        public GEM_Alarm(CSecsEquip300 m_pSecs, SecsEquipHelper secsEquipHelper)
        {
            this.m_pSecs = m_pSecs;
            this.SecsEquipHelper = secsEquipHelper;
        }
        public override void UpdateData()
        {
            
        }
        private int fsCount = 256;
        private int wsCount = 256;
        public List<bool> blnFs { get; set; } = new List<bool>(500);
        public List<bool> blnWs { get; set; } = new List<bool>(500);

        public List<bool> blnFs_Buf { get; set; } = new List<bool>(500);
        public List<bool> blnWs_Buf { get; set; } = new List<bool>(500);
        Task Alarmtask = null;
        public void SetAlarmList(List<bool> blnFs, List<bool> blnWs)
        {
            this.blnFs = blnFs;
            this.blnWs = blnWs;
            for (int i = 0; i < wsCount; i++)
            {
                if (this.blnFs_Buf.Count < fsCount)
                {
                    this.blnFs_Buf.Add(false);
                }
                if (this.blnWs_Buf.Count < wsCount)
                {
                    this.blnWs_Buf.Add(false);
                }
            }
            if (Alarmtask == null)
            {
                Alarmtask = Task.Run(new Action(AlarmUpdate));
            }
        }

        private void AlarmUpdate()
        {
            int inAlarmID = 50001;
            int inWarmingID = 50501;
            while (true)
            {
                for (int i = 0; i < fsCount; i++)
                {
                    if (blnFs[i] != blnFs_Buf[i] && blnFs[i])
                    {
                        AlarmSet(inAlarmID + i);
                        blnFs_Buf[i] = blnFs[i];
                    }
                    else if (blnFs[i] != blnFs_Buf[i] && blnFs_Buf[i])
                    {
                        AlarmReset(inAlarmID + i);
                        blnFs_Buf[i] = blnFs[i];
                    }
                }

                for (int i = 0; i < wsCount; i++)
                {
                    if (blnWs[i] != blnWs_Buf[i] && blnWs[i])
                    {
                        AlarmSet(inWarmingID + i);
                        blnWs_Buf[i] = blnWs[i];
                    }
                    else if (blnWs[i] != blnWs_Buf[i] && blnWs_Buf[i])
                    {
                        AlarmReset(inWarmingID + i);
                        blnWs_Buf[i] = blnWs[i];
                    }
                }
                Thread.Sleep(100);
            }
        }

        // 警报set
        void AlarmSet(int nID)
        {
            RcResult rc = m_pSecs.AlarmSet(nID, true);
        }

        // 警报clear
        void AlarmReset(int nID)
        {
            RcResult rc = m_pSecs.AlarmSet(nID, false);
        }

        // 使能
        public void Enable(int nALID)
        {
            m_pSecs.AlarmEnable(nALID, true);
        }

        // 去使能
        public void Disable(int nALID)
        {
            m_pSecs.AlarmEnable(nALID, false);
        }

        public void GetAlarmList(ref List<AlarmInfo>  listInfo)
        {
            m_pSecs.AlarmGetList(ref listInfo);
        }

    }

}

